package lib.self.util.sp;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import lib.self.AppEx;
import lib.self.ConstEx;
import lib.self.LogMgr;

/**
 * description: 存储数据工具类
 * author: Steven Xiao
 * date: 2017/4/26
 */
public class SpUtil {

    private static final String TAG = SpUtil.class.getSimpleName();

    public static boolean save(SharedPreferences sp, String key, Object value) {
        if (value instanceof Integer) {
            return save(sp, key, (Integer) value);
        } else if (value instanceof String) {
            return save(sp, key, (String) value);
        } else if (value instanceof Long) {
            return save(sp, key, (Long) value);
        } else if (value instanceof Boolean) {
            return save(sp, key, (Boolean) value);
        } else {
            LogMgr.e(TAG, "未知类型 = " + value);
            return false;
        }
    }

    public static boolean save(String spName, String key, Object value) {
        SharedPreferences sp = AppEx.getCt().getSharedPreferences(spName, Context.MODE_PRIVATE);
        return save(sp, key, value);
    }

    /**
     * 存储int
     */
    public static boolean save(SharedPreferences sp, String key, Integer value) {
        Editor editor = sp.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    /**
     * 存储long
     */
    public static boolean save(SharedPreferences sp, String key, Long value) {
        Editor editor = sp.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    /**
     * 存储string
     */
    public static boolean save(SharedPreferences sp, String key, String value) {
        Editor editor = sp.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    /**
     * 存储boolean
     */
    public static boolean save(SharedPreferences sp, String key, Boolean value) {
        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * 是否含有Key
     */
    public static boolean contains(SharedPreferences sp, String key) {
        return sp.contains(key);
    }

    /**
     * 获取int value
     */
    public static Integer getInt(SharedPreferences sp, String key) {
//        return sp.getInt(key, ConstEx.KErrNotFound);
        return sp.getInt(key, 0);//fixme 默认返回0
    }

    public static Integer getInt(SharedPreferences sp, String key, int defValue) {
        return sp.getInt(key, defValue);
    }

    public static Integer getInt(String spName, String key, int defValue) {
        SharedPreferences sp = AppEx.getCt().getSharedPreferences(spName, Context.MODE_PRIVATE);
        return getInt(sp, key, defValue);
    }

    /**
     * 获取 long value
     */
    public static Long getLong(SharedPreferences sp, String key) {
        return sp.getLong(key, ConstEx.KErrNotFound);
    }

    public static Long getLong(SharedPreferences sp, String key, long defValue) {
        return sp.getLong(key, defValue);
    }

    public static Long getLong(String spName, String key, long defValue) {
        SharedPreferences sp = AppEx.getCt().getSharedPreferences(spName, Context.MODE_PRIVATE);
        return getLong(sp, key, defValue);
    }

    /**
     * 获取 String value
     */
    public static String getString(SharedPreferences sp, String key) {
        return sp.getString(key, ConstEx.KEmptyValue);
    }

    public static String getString(SharedPreferences sp, String key, String defValue) {
        return sp.getString(key, defValue);
    }

    public static String getString(String spName, String key, String defValue) {
        SharedPreferences sp = AppEx.getCt().getSharedPreferences(spName, Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }

    /**
     * 获取 boolean value
     */
    public static boolean getBoolean(SharedPreferences sp, String key) {
        return sp.getBoolean(key, false);
    }

    public static boolean getBoolean(SharedPreferences sp, String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    public static boolean getBoolean(String spName, String key, boolean defValue) {
        SharedPreferences sp = AppEx.getCt().getSharedPreferences(spName, Context.MODE_PRIVATE);
        return getBoolean(sp, key, defValue);
    }

    /**
     * 清除数据
     */
    public static boolean clear(SharedPreferences sp) {
        Editor editor = sp.edit();
        editor.clear();
        return editor.commit();
    }

    public static boolean remove(SharedPreferences sp, String key) {
        Editor editor = sp.edit();
        editor.remove(key);
        return editor.commit();
    }

    public static boolean removeKeys(SharedPreferences sp, String... keys) {
        Editor editor = sp.edit();
        for (int i = 0; i < keys.length; i++) {
            editor.remove(keys[i]);
        }
        return editor.commit();
    }
}
